/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.editor;

import org.eclipse.gef.ContextMenuProvider;
import org.eclipse.gef.EditPartViewer;
import org.eclipse.gef.ui.actions.ActionRegistry;
import org.eclipse.gef.ui.actions.GEFActionConstants;
import org.eclipse.jface.action.ContributionItem;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionFactory;

import com.cea.papyrus.core.PapyrusMessages;
import com.cea.papyrus.core.actions.AdaptDimension;
import com.cea.papyrus.core.actions.ApplyStereotypeAction;
import com.cea.papyrus.core.actions.CloseDiagramAction;
import com.cea.papyrus.core.actions.delete.DeleteFromDiagramAction;
import com.cea.papyrus.core.actions.delete.DeleteFromModelAction;
import com.cea.papyrus.core.editpart.GraphNodeWithSemanticEditPart;
import com.cea.papyrus.core.editpart.LinkNameDisplayEditPart;


/**
 * Provides context menu actions for the Editor.
 */
public class EditorContextMenuProvider extends ContextMenuProvider {

	/**
	 * The editor's action registry.
	 * Darpan, changed from private to protected
	 */
	protected ActionRegistry actionRegistry;

	/**
	 * Instantiate a new menu context provider for the specified EditPartViewer
	 * and ActionRegistry.
	 * 
	 * @param viewer the editor's graphical viewer
	 * @param modelManager 
	 * @param registry the editor's action registry
	 * 
	 * @throws IllegalArgumentException if registry is <tt>null</tt>.
	 */
	public EditorContextMenuProvider(EditPartViewer viewer, ActionRegistry registry,ModelManager modelManager) {
		super(viewer);
		if (registry == null) {
			throw new IllegalArgumentException();
		}
		actionRegistry = registry;
	}

	/**
	 * Called when the context menu is about to show. Actions,
	 * whose state is enabled, will appear in the context menu.
	 * 
	 * @param menu 
	 * 
	 * @see org.eclipse.gef.ContextMenuProvider#buildContextMenu(org.eclipse.jface.action.IMenuManager)
	 */
	@Override
	public void buildContextMenu(IMenuManager menu) {
		GEFActionConstants.addStandardActionGroups(menu);

		IAction action;
		action = getActionRegistry().getAction(ActionFactory.UNDO.getId());
		menu.appendToGroup(GEFActionConstants.GROUP_UNDO, action);

		action = getActionRegistry().getAction(ActionFactory.REDO.getId());
		menu.appendToGroup(GEFActionConstants.GROUP_UNDO, action);
		
		action = getActionRegistry().getAction(AdaptDimension.ADAPT_DIMENSION_ACTION_ID);
		menu.appendToGroup(GEFActionConstants.GROUP_VIEW, action);
		
		action = getActionRegistry().getAction(DeleteFromModelAction.DELETE_FROM_MODEL_ACTION_ID);
		menu.appendToGroup(GEFActionConstants.GROUP_EDIT, action);
		
		action = getActionRegistry().getAction(DeleteFromDiagramAction.DELETE_FROM_DIAGRAM_ACTION_ID);
		menu.appendToGroup(GEFActionConstants.GROUP_EDIT, action);

		action = getActionRegistry().getAction(ActionFactory.COPY.getId());
		menu.appendToGroup(GEFActionConstants.GROUP_COPY, action);
		
		action = getActionRegistry().getAction(ActionFactory.PASTE.getId());
		menu.appendToGroup(GEFActionConstants.GROUP_COPY, action);
		
		action = getActionRegistry().getAction(CloseDiagramAction.CLOSE_DIAGRAM_ACTION_ID);
		if(action !=null) {
			((CloseDiagramAction) action).setSelection(getViewer().getSelection());
			menu.appendToGroup(GEFActionConstants.GROUP_SAVE, action);
		}
		
		
		// Alignment Actions
		MenuManager submenu = new MenuManager(PapyrusMessages.AlignmentAction_AlignSubmenu_ActionLabelText);

		action = getActionRegistry().getAction(GEFActionConstants.ALIGN_LEFT);
		if (action.isEnabled()) {
			submenu.add(action);
		}

		action = getActionRegistry().getAction(GEFActionConstants.ALIGN_CENTER);
		if (action.isEnabled()) {
			submenu.add(action);
		}

		action = getActionRegistry().getAction(GEFActionConstants.ALIGN_RIGHT);
		if (action.isEnabled()) {
			submenu.add(action);
		}
			
		submenu.add(new Separator());
		
		action = getActionRegistry().getAction(GEFActionConstants.ALIGN_TOP);
		if (action.isEnabled()) {
			submenu.add(action);
		}

		action = getActionRegistry().getAction(GEFActionConstants.ALIGN_MIDDLE);
		if (action.isEnabled()) {
			submenu.add(action);
		}

		action = getActionRegistry().getAction(GEFActionConstants.ALIGN_BOTTOM);
		if (action.isEnabled()) {
			submenu.add(action);
		}

		if (!submenu.isEmpty()) {
			menu.appendToGroup(GEFActionConstants.GROUP_VIEW, submenu);
		}
		
		// Profile management Actions
		submenu = new MenuManager("Profile");
		
		action = getActionRegistry().getAction(ApplyStereotypeAction.APPLY_STEREOTYPE_ACTION_ID);
		if(action.isEnabled()) {
			submenu.add(action);
		}
		
		if (!submenu.isEmpty()) {
			menu.appendToGroup(GEFActionConstants.GROUP_REST, submenu);
		}
		
		/* Other actions */
		if(!getViewer().getSelectedEditParts().isEmpty() 
				&& (getViewer().getSelectedEditParts().get(0) instanceof LinkNameDisplayEditPart)){
		
			menu.add(new ContributionItem(){
				MenuItem mi;				
				@Override
				public void fill(Menu menu, int index) {					
					int style= SWT.CHECK;
					
					mi= new MenuItem(menu, style, index);
					mi.setText("is visible");
					mi.setEnabled(true);
					mi.setAccelerator(SWT.ALT|'i');
					mi.setSelection(((LinkNameDisplayEditPart)getViewer().getSelectedEditParts().get(0)).isVisible());
					mi.setImage(PlatformUI.getWorkbench().getSharedImages().
							getImageDescriptor(org.eclipse.ui.ISharedImages.IMG_OBJS_INFO_TSK).
							createImage());
					mi.addSelectionListener(new SelectionAdapter() {
						@Override
						public void widgetSelected(SelectionEvent e) {
							GraphNodeWithSemanticEditPart ep=((GraphNodeWithSemanticEditPart)getViewer().getSelectedEditParts().get(0));
							ep.setVisible(!ep.isVisible());
						}
					});
				}
							
				@Override
				public boolean isDynamic() {
					return false;
				}
			});
		}
		

		
		
		
	}
	
	/**
	 * 
	 * 
	 * @return 
	 */
	protected ActionRegistry getActionRegistry() {
		return actionRegistry;
	}
}
